import { defineStore } from 'pinia'
//@ts-ignore
import type { UserInfo } from '@/types/auth'



export const useAuthStore = defineStore('auth', {

  state: () => ({

    token: localStorage.getItem('token') || '',

    userInfo: JSON.parse(localStorage.getItem('userInfo') || 'null') as UserInfo | null,
    isAdmin: false
  }),



  getters: {

    isAuthenticated: (state) => !!state.token

  },



  actions: {

    login(token: string, userInfo: UserInfo, brandId: number) {

      this.token = token

      this.userInfo = userInfo
      this.isAdmin = brandId === 0

      localStorage.setItem('token', token)

      localStorage.setItem('userInfo', JSON.stringify(userInfo))

    },



    logout() {

      this.token = ''

      this.userInfo = null
      this.isAdmin = false
      localStorage.removeItem('token')

      localStorage.removeItem('userInfo')

    }

  }

})
